#pragma once
#include "CBondSource.h"
#include "BrokerKeyInfo.h"
#include "JumpListDefines.h"
#include "../SSSortInfo.h"
#include <vector>

class CBondFilter;
class CBondComparator;
class CBondSorter;
////////////////////////////////////////////////////////////////////////////////

class BrokerKeyJumpList;
class BrokerKeyLevelList;
class BrokerKeyListImpl;
class S_BONDLIB_EXPORT CBrokerKeyList : public IBrokerKeyDest{// 类似 CSSVContainer的功能
	BrokerKeyListImpl*	m_keys;				//
	CSortInfoManager	m_sort_info;		// 当前记录的排序信息,决定一条记录如何与另一条记录进行比较
	CBondSorter*		m_bond_sorter;
	int					m_searchType;
	std::string			m_searchString;
public:
	CBrokerKeyList(const CSortInfoManager& sortInfo,int brokerid);
	~CBrokerKeyList();
	CSortInfoManager& GetSortInfo(){ return m_sort_info; }
	const CSortInfoManager& GetSortInfo()const{ return m_sort_info; }

	void SetSearchString(int nType, const char* sSearch);

	//==============================================================================
	// Summary:
	//     -compare two BrokerKey
	// Parameters:
	//     -
	// Return:
	//     -
	// Remarks:
	//     -
	// Usage:
	//     -used by JumpList:m_keys to decide find insert position
	//==============================================================================
	int CompareBond(const BrokerKeyInfo& b1, const BrokerKeyInfo& b2);

	//==============================================================================
	// Summary:
	//     -
	// Parameters:
	//     -bid			: index of CBondContainer::m_vctbond
	//     -brokerid	: BrokerKey.GetBrokerId()
	//     -context		: SSBrokerKeyVector pointer
	// Return:
	//     -
	// Remarks:
	//     -
	// Usage:
	//     -used by CBondFilter to add BrokerKey to this container
	//==============================================================================
	virtual void OnBrokerKeyFound(BondID bid, const char* brokerid, void* context);

	//==============================================================================
	// Summary:
	//     -clear all records and fetch all records again.
	// Parameters:
	//     -source	: decide how bond index is accessed
	//     -filter	: decide which BrokerKey is valid according to current filter info
	// Return:
	//     -
	// Remarks:
	//     -
	// Usage:
	//     -used when filter info changed
	//==============================================================================
	virtual void Reloads(CBondSource* source, CBondFilter* filter);
	virtual void ReloadsCFETS(int nType, CBondSource* source, CBondFilter* filter);
	virtual void ReloadsCFETSDeal(CBondSource* source, CBondFilter* filter);

	virtual void ReloadsSSEFI(CBondSource* source, CBondFilter* filter);
	//==============================================================================
	// Summary:
	//     -sort all records in this container so as to keep in order
	// Parameters:
	//     -
	// Return:
	//     -
	// Remarks:
	//     -
	// Usage:
	//     -
	//==============================================================================
	virtual void Sort();

	virtual void Sort(BYTE btSettlement);//5.1.1 add by lance, 用于最优报价带清算速度排序

	//==============================================================================
	// Summary:
	//     -add or remove BrokerKeys from this container and keep left in order
	// Parameters:
	//     -keys	: 
	//     -add		: true means add keys to this container,false means remove keys from
	//                this container
	// Return:
	//     -
	// Remarks:
	//     -it is unnecessary to sort all records when Update. All records after Update
	//     -are kept in order.
	//     -This method use accelerated data structures to find where to insert so as to 
	//     -keep all records in order.Interesting? See JumpList::FindInsertPosition.
	// Usage:
	//     -used when some CBondInfo's some field is updated,you want to keep records in
	//     -this container in order
	//     -or when you want to add or remove some records from this container and still 
	//     -want to keep all records in this container in order
	//==============================================================================
private:
	void Update(const CBrokerKeyVector& keys, bool add);
	void Update(const BrokerKey& key, bool add);
public:
	void Update(const CBrokerKeyVector& addKeys, const CBrokerKeyVector& eraseKeys);
	void UpdateBatch(const std::vector<BrokerKeyInfo>& keys, const std::vector<bool>& addOrErase);

	//==============================================================================
	// Summary:
	//     -fetch count of all BrokerKey in this container
	//==============================================================================
	int GetRowCount();

	//==============================================================================
	// Summary:
	//     -fetch one screen BrokerKey for Grid control to show
	// Parameters:
	//     -row		: start index in this container
	//     -count	: number of BrokerKey to fetch
	//     -keys	: fetched BrokerKeys
	// Return:
	//     -
	// Remarks:
	//     -BrokerKeys of this container are always in order.
	// Usage:
	//     -used when you need update list of grid control
	//==============================================================================
	int GetRecordKeys(int row, int count, BrokerKeyInfo* keys);

	bool IsExist(const BrokerKeyInfo& key);
	int  GetVersion(const BrokerKeyInfo& key);
protected:
	kJumpInsertWhere GetInsertWhere();
	void SortImpl(std::vector<BrokerKeyInfo>& keys, bool force = false);
	void SortImpl(std::vector<BrokerKeyInfo>& keys, BYTE btSettlement, bool force = false);
	void PrintModifyTimes(const char* brokerid,bool before);
};